package com.qu.waibao.home.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseActivity;

import org.xml.sax.helpers.DefaultHandler;

import butterknife.BindView;
import butterknife.OnClick;
import info.wangchen.simplehud.SimpleHUD;

public class WebActivity extends BaseActivity implements View.OnClickListener{
    private String url = "";
    @BindView(R.id.mWebView)
    WebView mWebView;
    @BindView(R.id.iv_publish)
    ImageView iv_publish;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.fl_container)
    FrameLayout fl_container;
    private String title = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setWindowStatusBarColor(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean initBundle(Bundle bundle) {
        if(bundle != null){
            title = bundle.getString("title","");
        }
        return super.initBundle(bundle);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_web;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tv_title.setText(title);
        iv_publish.setVisibility(View.GONE);
        mWebView.getSettings().setJavaScriptEnabled(true);
        fl_container.setBackgroundColor(getResources().getColor(R.color.yellow));
    }

    @Override
    protected void initData() {
        super.initData();
        String userid = AppConfig.getString("userid");
        if(title.equals("抽奖")){
            url = "http://clc.027perfect.com/pf_publictmp.php?act=publictmp&do=zhuanpan&uid="+userid;
        }else if(title.equals("签到")){
            url = "http://clc.027perfect.com/pf_publictmp.php?act=publictmp&do=qiandao&uid="+userid;
        }
        mWebView.setWebViewClient(new MyWebClient());
        mWebView.loadUrl(url);
    }
    @OnClick({R.id.ll_back})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mWebView != null){
            mWebView.destroy();
        }
    }
    private class MyWebClient extends WebViewClient{
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            SimpleHUD.showLoadingMessage(WebActivity.this,"",true);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            SimpleHUD.dismiss();
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            SimpleHUD.dismiss();
        }
    }

    public void setWindowStatusBarColor(Activity activity) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(R.color.yellow));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
